package com.mxpio.erp.equipment.controller;


import com.mxpio.erp.common.equipment.entity.EquipmentRepairEvaluateDetail;
import com.mxpio.erp.common.equipment.service.EquipmentRepairEvaluateDetailService;
import com.mxpio.erp.common.equipment.service.EquipmentRepairService;
import com.mxpioframework.common.vo.Result;
import com.mxpioframework.jpa.query.Criteria;
import com.mxpioframework.jpa.query.Operator;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

import java.io.UnsupportedEncodingException;
import java.util.List;

@Tag(name = "EquipmentRepairEvaluateDetailController", description = "设备维修-维修评价")
@RestController
@RequestMapping("/erp/equipment/spotrepair/evaluate/detail/")
public class EquipmentRepairEvaluateDetailController {
    @Autowired
    private EquipmentRepairEvaluateDetailService service;


    @Autowired
    private EquipmentRepairService repairService;
    @GetMapping("list")
    @Operation(summary = "设备维修-维修评价列表", description = "获取设备维修-维修评价列表（无分页）", method = "GET")
    public Result<List<EquipmentRepairEvaluateDetail>> list(@Parameter(description="查询参数") Criteria criteria) throws UnsupportedEncodingException {
        List<EquipmentRepairEvaluateDetail> list = service.list(EquipmentRepairEvaluateDetail.class, criteria);
        return Result.OK(list);
}

    @GetMapping("page")
    @Operation(summary = "设备维修-维修评价列表", description = "获取设备维修-维修评价列表（分页）", method = "GET")
    public Result<Page<EquipmentRepairEvaluateDetail>> page(@Parameter(description="查询参数") Criteria criteria,
                                                            @Parameter(description="每页条数")Integer pageSize,
                                                            @Parameter(description="页号")Integer pageNo) throws UnsupportedEncodingException {
        Pageable page = PageRequest.of(pageNo-1, pageSize);
        Page<EquipmentRepairEvaluateDetail> pageResult = service.listPage(EquipmentRepairEvaluateDetail.class, page, criteria);
        return Result.OK(pageResult);
    }

    @GetMapping("list/{bizNo}")
    @Operation(summary = "根据bizNo工单编码获取设备维修-维修评价-列表", description = "根据bizNo工单编码获取设备维修-维修评价-列表", method = "GET")
    public Result<List<EquipmentRepairEvaluateDetail>> getById(@PathVariable(name = "bizNo", required = true) String bizNo) {
        Criteria criteria = Criteria.create();
        criteria.addCriterion("bizNo", Operator.EQ,bizNo);
        List<EquipmentRepairEvaluateDetail> list = service.list(EquipmentRepairEvaluateDetail.class, criteria);
        return Result.OK(list);
    }

    @RequestMapping("add")
    @Operation(summary = "保存设备维修-维修评价", description = "保存设备维修-维修评价")
    public Result<EquipmentRepairEvaluateDetail> add(@RequestBody EquipmentRepairEvaluateDetail equipmentRepairEvaluateDetail) {
        service.save(equipmentRepairEvaluateDetail);
        return Result.OK(equipmentRepairEvaluateDetail);
    }

    @RequestMapping(value = "save",method = RequestMethod.POST)
    @Operation(summary = "保存设备维修-维修评价", description = "保存设备维修-维修评价")
    public Result<List<EquipmentRepairEvaluateDetail>> save(@RequestBody List<EquipmentRepairEvaluateDetail> details) {
        return repairService.saveDetailsAndEvaluta(details);
    }

}
